var express = require('express');
var router = express.Router();
var shopController = require("../controller/shopController")



//获取所有商铺信息
router.get("/getAll", async (req, res, next) => {
    var data = await shopController.getall()
    res.json({
        code: 1,
        msg: "获取所有商铺信息成功",
        data: data
    })
});

//获取单个商铺数据
router.get("/get/:shopname",async (req,res,next)=>{
    var params = req.params
    var data = await shopController.query(params)
    res.json({
        code:1,
        msg:"获取成功",
        data
    })
})

//添加商铺信息
router.post("/add",(req, res, next) => {
    let body = req.body;
    let insert = shopController.add(body)
    res.json({
        code: 1,
        msg: "添加商铺信息成功",
        insert
    })
})

//修改商铺信息接口
router.put("/modify/:shopname",async (req, res, next) => {
    let data = req.body;
    let params = req.params;
    let result = await shopController.update(data,params)
    if(result.n>=1){
        res.json({
            code: 1,
            msg: "更新成功",
        })
    }else{
        res.json({
            code: 1,
            msg: "没有更新任何数据",
        })
    }
})

//删除商铺信息
router.delete("/delete/:shopname",async (req,res,next)=>{
    let params = req.params;
    let result = await shopController.delete(params)
    if(result.n>=1){
        res.json({
            code:1,
            msg:"删除成功",
        })
    }else{
        res.json({
            code:0,
            msg:"删除失败",
        })
    }
});




module.exports = router;
